package com.atguigu.edu.controller;


import com.atguigu.edu.entity.Teacher;
import com.atguigu.edu.entity.vo.TeacherQuery;
import com.atguigu.edu.service.TeacherService;


import com.atguigu.utils.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author ld
 * @since 2022-08-10
 */
//@CrossOrigin //跨域
@RestController
@RequestMapping("/eduservice/teacher")
@Api(description = "讲师管理")
public class TeacherController {

    @Resource
    private TeacherService teacherService;

    @ApiOperation(value = "查询所有的讲师信息")
    @GetMapping("/findAll")
    public R list(){
        List<Teacher> list = teacherService.list(null);
//        try {
//            int a=10/0;
//        } catch (Exception e) {
//            throw new CustomException(500,"执行了自定义异常处理");
//        }
        return R.ok().data("items",list);
    }

    @ApiOperation(value = "根据id删除讲师")
    @DeleteMapping("/delete/{id}")
    public R removeById(@ApiParam(value = "讲师唯一id",required = true,name = "id") @PathVariable("id") String id){
        boolean flag = teacherService.removeById(id);
        System.out.println(flag);// 代码不出错就为true,没有这个数据去删除也不会报错
        if(flag){
            return R.ok();
        }else{
           return R.error();
        }
    }
    @ApiOperation(value = "分页查询讲师列表")
    @GetMapping("/page/{current}/{size}")
    public R page(@ApiParam(name = "current", value = "当前页码", required = true)
                      @PathVariable("current") Long current,
                  @ApiParam(name = "size", value = "每页记录数", required = true)
                      @PathVariable("size") Long size){
        Page<Teacher> pageInfo=new Page<>(current,size);
        teacherService.page(pageInfo,null);
        long total = pageInfo.getTotal();
        List<Teacher> records = pageInfo.getRecords();
        return R.ok().data("total",total).data("rows",records);
    }
    @ApiOperation(value = "分页组合条件查询讲师列表")
    @PostMapping("/page/{current}/{size}")
    public R pageQuery(@ApiParam(name = "current", value = "当前页码", required = true)
                  @PathVariable("current") Long current,
                       @ApiParam(name = "size", value = "每页记录数", required = true)
                  @PathVariable("size") Long size,
                       @RequestBody(required = false) TeacherQuery teacherQuery){

        Page pageInfo = teacherService.pageQuery(current, size, teacherQuery);
        long total = pageInfo.getTotal();
        List<Teacher> records = pageInfo.getRecords();
        return R.ok().data("total",total).data("rows",records);
    }
    @ApiOperation(value = "添加讲师")
    @PostMapping("/add")
    public R add(@RequestBody Teacher teacher){
        boolean save = teacherService.save(teacher);
        if(save){
            return R.ok();
        }
        return R.error();
    }
    @ApiOperation(value = "根据id查询讲师")
    @GetMapping("/list/{id}")
    public R listById(@PathVariable("id") String id){
        Teacher t = teacherService.getById(id);
        return R.ok().data("item",t);
    }
    @ApiOperation(value = "根据id修改讲师")
    @PutMapping("/update")
    public R updateById(@RequestBody Teacher teacher){
        boolean b = teacherService.updateById(teacher);
        if(b){
            return R.ok();
        }
        return R.error();
    }
}

